package com.bestv.engine.recommender;

import com.bestv.database.vo.Base;
import com.bestv.database.vo.Series;
import com.bestv.engine.filter.*;
import com.bestv.engine.model.RecommendationItem;
import com.bestv.engine.model.RecommendationModel;
import com.bestv.engine.model.RecommendationRule;
import com.bestv.engine.sorter.GenreSorter;
import com.bestv.engine.sorter.Sorter;
import com.bestv.engine.sorter.Sorters;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * Created by Henry on 14-5-23.
 */
public class Recommender {

    private String name;
    private List<Model> modelChain;
    private Sorters sorterChain;

    public RecommendationRule recommend(Base base, int maxCount) {
        return recommend(base, maxCount, null, false);
    }

    public RecommendationRule recommend(Base base, int maxCount, Filters filters) {
        return recommend(base, maxCount, filters, false);
    }

    public RecommendationRule recommend(Base base, int maxCount, Filters filters, boolean isByGenre) {
        RecommendationRule rule = new RecommendationRule();
        rule.setMaxSize(maxCount);

        int typeId = 2;
        String seriesName = null;
        String seriesGenre = null;
        if (base instanceof Series) {
            Series series = (Series) base;
            typeId = series.getTypeId();
            seriesName = series.getName();
            seriesGenre = series.getGenre();
            Filter typeFilter = new TypeFilter(typeId);
            filters.add(typeFilter);

            if (StringUtils.isNotBlank(seriesName)) {
                Filter nameFilter = new NameFilter(seriesName);
                filters.add(nameFilter);
            }

            if (false && isByGenre) {
                String tags = String.format("%s;%s;%s;%s", series.getTags(), series.getActors(), series.getDirector(), series.getKeywords());
                Filter tagsFilter = new TagsFilter(tags);
                filters.add(tagsFilter);
            }
        }

        if (modelChain == null) return rule;
        for (Model model : modelChain) {
            if (rule.isFull()) break;
            RecommendationModel recommendationModel = model.getRecommendationModel();
            List<RecommendationItem> items = recommendationModel.recommend(base.getId(), typeId, model.getSize());
            rule.addAll(items, model.getSize(), filters);
        }

        if (isByGenre && seriesGenre != null) {
            Sorter genreSorter = new GenreSorter(seriesName, seriesGenre);
            genreSorter.sort(rule.getRecommendationItemList());
        }
        if (sorterChain != null) sorterChain.sort(rule);
        return rule;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setModelChain(List<Model> modelChain) {
        this.modelChain = modelChain;
    }

    public void setSorterChain(Sorters sorterChain) {
        this.sorterChain = sorterChain;
    }
}
